package com.mlm.mlmapp.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.flyco.tablayout.view.FixedSlidingTabLayout;
import com.flyco.tablayout.view.OrderFragment;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.interfaces.Prefs_;
import com.mlm.mlmapp.utils.CommonUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.Receiver;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.DimensionRes;
import org.androidannotations.annotations.res.DrawableRes;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
@EActivity(R.layout.orders)
public class OrdersActivity extends BaseActivity implements HTTPCons {
    public static final int TYPE_ALL = 0;
    public static final int TYPE_NONPAY = 1;
    public static final int TYPE_BOOKED = 2;
    public static final int TYPE_FINISH = 6;
    public static final int TYPE_REFUND = 7;
    @Extra
    int flag;
    @Pref
    Prefs_ pref;
    @DimensionRes(R.dimen.recyclerview_mycomments_space)
    float commentImagesMargin;
    @DimensionRes
    float comments_images_edge;
    @ColorRes
    int red;
    @ColorRes
    int lightgreytext;
    @ColorRes
    int blacktext;
    public String currentJson = "";
    private final String[] mTitles = {"全部", "未支付", " 已支付", "已完成", "退款"};
    @ViewById
    TextView tv_title;
    @ViewById
    TextView tv_no_title;
    @ViewById
    FixedSlidingTabLayout tl;
    private AQuery aq;
    List<Integer> orderlist;
    List<JSONObject> ordersDateset;
    @DrawableRes
    Drawable grey_edge_btn_long;
    @ViewById
    ViewPager vp;
    @ViewById
    RadioGroup top_group;
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    @AfterViews
    void init() {
        String title = getTitle().toString();
        if (!"".equals(title)) {
            ((TextView) this.findViewById(R.id.tv_title)).setText(title);
        }
        vp.setAdapter(new MyPagerAdapter(this.getSupportFragmentManager()));
        vp.setOffscreenPageLimit(5);
        vp.setKeepScreenOn(true);
        mFragments.add(OrderFragment
                .getInstance(pref.loginID().get(), TYPE_ALL));
        mFragments.add(OrderFragment.getInstance(pref.loginID().get(),
                TYPE_NONPAY));
        mFragments.add(OrderFragment.getInstance(pref.loginID().get(),
                TYPE_BOOKED));
        mFragments.add(OrderFragment.getInstance(pref.loginID().get(),
                TYPE_FINISH));
        mFragments.add(OrderFragment.getInstance(pref.loginID().get(),
                TYPE_REFUND));
        vp.getAdapter().notifyDataSetChanged();
        tl.setViewPager(vp, mTitles);
        switch (flag) {
            case TYPE_ALL:
                vp.setCurrentItem(0);
                break;
            case TYPE_NONPAY:
                vp.setCurrentItem(1);
                break;
            case TYPE_BOOKED:
                vp.setCurrentItem(2);
                break;
            case TYPE_FINISH:
                vp.setCurrentItem(3);
                break;
            case TYPE_REFUND:
                vp.setCurrentItem(4);
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        Intent intent = new Intent();
        this.setResult(RESULT_CANCELED, intent);
        CommonUtils.sendBroadCast(OrdersActivity.this,"point_refresh", "", "");
        this.finish();
    }

    @Click
    void btn_back() {
        onBackPressed();
    }

    @OnActivityResult(11)
    void onCommentResult(int resultcode, Intent data) {
        if (resultcode != RESULT_CANCELED) {
            if (data != null) {
                if (data.getBooleanExtra("flag", false)) {
                    for (Fragment f : mFragments) {
                        ((OrderFragment) f).refresh();
                    }
                }
            }
        }
    }

    @Receiver(actions = OrderFragment.ORDER_REFRESH_FLAG, registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
    protected void onOrderRefresh(Intent intent) {
        if (intent.getStringExtra("refreshflag") != null && "0".equals(intent.getStringExtra("refreshflag"))) {
            for (Fragment f : mFragments) {
                ((OrderFragment) f).refresh();
            }
        }


    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "";
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
